<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    Admin_Model
 * @subpackage PriceRule
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The domain for price rule
 * 
 * @category   Zend
 * @package    Admin_Model
 * @subpackage PriceRule
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Admin_Model_PriceRule extends Zend_Db_Table_Abstract
{

    /**
     * The name of table
     * @var string 
     */
    protected $_name = 'price_rule';

    /**
     * Save price rule
     * @param array $row
     */
    public function save($row)
    {
        // check ID (insert or update)
        $id = isset($row['id']) ? $row['id'] : null;
        if ($id) {
            unset($row['id']);
            $this->update($row, array('id = ?' => $id));
        } else {
            $this->insert($row);
        }
    }

    /**
     * Get price rules by filter 
     * @return array 
     */
    public function getAllByFilter($categoryName, $manufacturerName, $productName, $markupFrom, $markupTo, $priorityFrom, $priorityTo, $updatedFrom, $updatedTo, $languageId, $sort, $direction, $page, $rowCount)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('pr' => $this->_name), array('pr.id', 'pr.markup', 'pr.priority', 'pr.updated'));
        $query->joinLeft(array('c' => 'category'), 'pr.category_id = c.id', '');
        $query->joinLeft(array('ct' => 'category_translation'), $this->getAdapter()->quoteInto('ct.category_id = c.id AND ct.language_id = ?', $languageId), 'ct.name as category_name');
        $query->joinLeft(array('m' => 'manufacturer'), 'pr.manufacturer_id = m.id', 'm.name as manufacturer_name');
        $query->joinLeft(array('p' => 'product'), 'pr.product_id = p.id', 'p.name as product_name');

        // Check filters
        if ($categoryName) {
            $query->where('ct.name LIKE ?', $categoryName . '%');
        }
        if ($manufacturerName) {
            $query->where('m.name LIKE ?', $manufacturerName . '%');
        }
        if ($productName) {
            $query->where('p.name LIKE ?', $productName . '%');
        }
        if ($markupFrom) {
            $query->where('pr.markup >= ?', $markupFrom);
        }
        if ($markupTo) {
            $query->where('pr.markup <= ?', $markupTo);
        }
        if ($priorityFrom) {
            $query->where('pr.priority >= ?', $priorityFrom);
        }
        if ($priorityTo) {
            $query->where('pr.priority <= ?', $priorityTo);
        }
        if ($updatedFrom) {
            $query->where('pr.updated >= ?', $updatedFrom);
        }
        if ($updatedTo) {
            $query->where('pr.updated <= ?', $updatedTo);
        }

        if ($sort && $direction) {
            $query->order($sort . ' ' . $direction);
        }

        $query->limitPage($page, $rowCount);

        // Get priceRules from DB
        $result = $this->getAdapter()->fetchAll($query, null, Zend_Db::FETCH_OBJ);
        return $result;
    }

    /**
     * Get count of priceRules by filter 
     * @return integer 
     */
    public function getCountByFilter($categoryName, $manufacturerName, $productName, $markupFrom, $priorityFrom, $priorityTo, $markupTo, $updatedFrom, $updatedTo, $languageId)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('pr' => $this->_name), new Zend_Db_Expr('COUNT(*)'));
        $query->joinLeft(array('c' => 'category'), 'pr.category_id = c.id', '');
        $query->joinLeft(array('ct' => 'category_translation'), $this->getAdapter()->quoteInto('ct.category_id = c.id AND ct.language_id = ?', $languageId), '');
        $query->joinLeft(array('m' => 'manufacturer'), 'pr.manufacturer_id = m.id', '');
        $query->joinLeft(array('p' => 'product'), 'pr.product_id = p.id', '');

        // Check filters
        if ($categoryName) {
            $query->where('ct.name LIKE ?', $categoryName . '%');
        }
        if ($manufacturerName) {
            $query->where('m.name LIKE ?', $manufacturerName . '%');
        }
        if ($productName) {
            $query->where('p.name LIKE ?', $productName . '%');
        }
        if ($markupFrom) {
            $query->where('pr.markup >= ?', $markupFrom);
        }
        if ($markupTo) {
            $query->where('pr.markup <= ?', $markupTo);
        }
        if ($priorityFrom) {
            $query->where('pr.priority >= ?', $priorityFrom);
        }
        if ($priorityTo) {
            $query->where('pr.priority <= ?', $priorityTo);
        }
        if ($updatedFrom) {
            $query->where('pr.updated >= ?', $updatedFrom);
        }
        if ($updatedTo) {
            $query->where('pr.updated <= ?', $updatedTo);
        }

        // Get count of price rules from DB
        $result = $this->getAdapter()->fetchOne($query);
        return $result;
    }

    /**
     * Get price rule by ID
     * @param integer $id
     * @return object 
     */
    public function getById($id)
    {
        $query = $this->getAdapter()->select();
        $query->from($this->_name, array('id', 'product_id', 'category_id', 'manufacturer_id', 'markup', 'priority'));
        $query->where('id = ?', $id);

        // Get price rule from DB
        $result = $this->getAdapter()->fetchRow($query, null, Zend_Db::FETCH_OBJ);
        return $result;
    }

    /**
     * Get all price rules
     * @return array 
     */
    public function getAll()
    {
        $query = $this->getAdapter()->select();
        $query->from($this->_name, array('product_id', 'category_id', 'manufacturer_id', 'markup', 'price'));
        $query->order('priority');
        $result = $this->getAdapter()->fetchAll($query, null, Zend_Db::FETCH_OBJ);
        return $result;
    }

}
